// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import API_POINT from './API_POINT'

// ============================== 导出异步调用 ============================== //

export default {
  // —————————————————————————————— 单项 —————————————————————————————— //

  async remoteOne({ state: { API, one, MAP } }) {
    if (one.isGraph) {
      const TIP = API.transTip('查询系统图')
      const obj = await API_POINT.getOne(one, TIP)
      return { ...obj.deviceHospitalPointDto, ...one, deviceGraphDto: obj.deviceGraphDto }
    } else {
      const TIP = API.transTip(MAP.tipOne)
      const obj = await API.getOne(one, TIP)
      return transOne(obj)
    }
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async remoteEdit({ state: { API, one, MAP } }) {
    if (one.isGraph) {
      const TIP = API.transTip('绑定系统图')
      one.deviceGraphDto.forEach((item, i) => (item.sort = i + 1))
      const msg = await API_POINT.bind(one, TIP)
      return msg
    } else {
      const msg = await API.edit(one, '保存设备')
      if (CORE.MAP.MAP_SAME_AREA && one.areaId !== one.oldAreaId) { // 删除打点
        await API.mapDel({ deviceId: one.id, deviceCategoryId: one.deviceCategoryId })
      }
      return msg
    }
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async remoteSubmit({ state: { API, one } }) {
    const [addNum, editNum] = await API.submit(one)
    Object.assign(one, { addNum, editNum })
  },
}

// ============================== 转换方法 ============================== //

const transOne = (obj) => {
  obj.oldAreaId = obj.areaId
  obj.tagIdList = obj.deviceLabelDtos.map(({ labelId }) => labelId)
  return obj
}
